//
//  WFLiveViewController.m
//  WangYiFirst
//
//  Created by 蔡靖方 on 16/9/11.
//  Copyright © 2016年 111. All rights reserved.
//

#import "WFLiveViewController.h"
#import "WFcategoryTableViewController.h"
#import "WFlHotliveTableViewController.h"
@interface WFLiveViewController ()<UIScrollViewDelegate>
//导航栏内容
@property(nonatomic,strong)UIView *titleviews;
//导航栏内容
@property(nonatomic,strong)NSArray *arr;
//记录上次点击按钮
@property(nonatomic,strong)UIButton *buttonRemb;
//创建点击内容的scroll
@property(nonatomic,strong)UIScrollView *scrollContent;
@end

@implementation WFLiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
    //添加导航类标签
    [self titleNavbar];
    
    //加载内容自控制器
    [self addtabview];
    //添加内容的scorllview
    [self addscrollview];
    //进来选中hot业
    [self Click:self.titleviews.subviews.firstObject];
    self.scrollContent.delegate=self;
    
    
    // Do any additional setup after loading the view.
}
-(void)addtabview{
    [self addChildViewController:[[WFlHotliveTableViewController alloc]init]];
    [self addChildViewController:[[WFcategoryTableViewController alloc]init]];
    
    
    
}
#pragma mark 添加导航标签
-(void)titleNavbar{
    self.arr=@[@"热门",@"分类"];
    self.titleviews=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
    
    self.titleviews.layer.cornerRadius= _titleviews.frame.size.height/2;
    
    _titleviews.layer.borderWidth=1.0;
    self.titleviews.layer.borderColor=[UIColor grayColor].CGColor;
    //    self.titleviews.layer.masksToBounds = YES;
    
    
    self.titleviews.backgroundColor=[UIColor redColor];
    
    for (int i=0; i<self.arr.count; i++) {
        UIButton *titlebutton=[UIButton buttonWithType:UIButtonTypeCustom];
        titlebutton.frame=CGRectMake(i*75, 0, 75, 30);
        titlebutton.layer.cornerRadius=titlebutton.frame.size.height/2;
        //        titlebutton.highlighted=NO;
        //取消按钮高亮
        [titlebutton setAdjustsImageWhenHighlighted:NO];
        [titlebutton setTitle:self.arr[i] forState:UIControlStateNormal];
        [titlebutton setTitle:self.arr[i] forState:UIControlStateSelected];
        
        [titlebutton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        //         titlebutton.backgroundColor=[UIColor blueColor];
        titlebutton.tag=i;
        [titlebutton addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleviews addSubview:titlebutton];
    }
    self.navigationItem.titleView=self.titleviews;
    
    
    
}
#pragma mark 监听导航标签单机
-(void)Click:(UIButton *)sender{
    self.buttonRemb.selected=NO;
    
    sender.selected=YES;
    if (sender.selected) {
        sender.backgroundColor=[UIColor whiteColor];
        //设置button的宽度和颜色
        sender.layer.borderWidth=1.0;
        //
        sender.layer.borderColor=[UIColor grayColor].CGColor;
    }
    if (self.buttonRemb.selected==NO) {
        self.buttonRemb.backgroundColor=[UIColor clearColor];
        self.buttonRemb.layer.borderColor=[UIColor clearColor].CGColor;
    }
    
    self.buttonRemb=sender;
    
    
    //设置偏移量  之后才个以改变crollViewDidEndScrollingAnimation的bands 才可以创分类业
    self.scrollContent.contentOffset = CGPointMake(sender.tag*self.scrollContent.frame.size.width, 0);
    
}
#pragma mark 添加内容的scroll
-(void)addscrollview{
    _scrollContent=[[UIScrollView alloc]init];
    _scrollContent.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _scrollContent.contentSize=CGSizeMake(self.childViewControllers.count*self.view.frame.size.width, 0);
    for (int i=0; i<self.arr.count; i++) {
        self.childViewControllers[i].view.frame=CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
        [self.scrollContent addSubview:self.childViewControllers[i].view];
    }
    self.scrollContent.pagingEnabled=YES;
    self.scrollContent.showsHorizontalScrollIndicator=NO;
    _scrollContent.backgroundColor=[UIColor yellowColor];
    
    [self.view addSubview:_scrollContent];
    
    
}
#pragma mark  拖动换业
/**
 * 当减速完毕的时候调用（人为拖拽scrollView，手松开后scrollView慢慢减速完毕到静止）
 */
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //计算scrooll的偏移量
    CGFloat lenth=scrollView.contentOffset.x;
    NSInteger unm=lenth/self.view.frame.size.width;
    [self Click:self.titleviews.subviews[unm]];
    
    
    
    
    
}
//-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
//    NSLog(@"%@",scrollView);
//}
//#pragma mark - <UIScrollViewDelegate>
///**
// * 当滚动动画完毕的时候调用（通过代码setContentOffset:animated:让scrollView滚动完毕后，就会调用这个方法）
// * 如果执行完setContentOffset:animated:后，scrollView的偏移量并没有发生改变的话，就不会调用scrollViewDidEndScrollingAnimation:方法
// */
//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
//{
//    // 取出对应的子控制器
//    int index = scrollView.contentOffset.x /scrollView.frame.size.width;
//    //看一下分类的是否被穿件过
//    UIViewController *willShowChildVc = self.childViewControllers[index];
//
//    // 如果控制器的view已经被创建过，就直接返回
//    if (willShowChildVc.isViewLoaded) return;
//
//    // 添加子控制器的view到scrollView身上
//    willShowChildVc.view.frame = self.scrollContent.bounds;
//    NSLog(@"%f",scrollView.bounds.size.width);
//    [scrollView addSubview:willShowChildVc.view];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
